use Mojo::mysql;
use strict;

my $mysql = Mojo::mysql->strict_mode('mysql://dahama:xxxxxxxxxx@/vpninfo');

my $db = $mysql->db;

sub check_004 {
    my $user = shift;
    my $company_id = shift;
    my $results = $db->query('select sbu_id,userrole from admin_account where username=?',$user)->array;
    my $sbu_id = $results->[0];
    my $userrole = $results->[1];
    my $map_results = $db->query('select number from sbu_company_relation where company_id=? AND role=? AND sbu=?',$company_id,$userrole,$sbu_id)->array;
    if ($map_results->[0]){
        return 1;
    }else{
        return 0;
    };
}

sub check_005 {
    my $company_id = shift;
    my $user = shift; #修改为从session中获得！
    my $map_results = $db->query('select * from sbu_company_relation where company_id=?',$company_id)->hash;
    if ($map_results->{number}){
        print "您要添加的公司已由$map_results->{sbu}（事业部）注册，正在为您创建新的事业部关系，稍后请重新查询\n";
        my $results = $db->query('select sbu_id,userrole from admin_account where username=?',$user)->array;
        my $sbu = $results->[0];
        my $role = $results->[1];
        $db->query('insert into sbu_company_relation (company_id,sbu,role) values (?,?,?)',$company_id,$sbu,$role);
        return 0; #修改返回成功。
    }else{
        return 1;
    }
}

sub show_company_info {
    my $company_id = shift;
    my $hash = $db->query('select * from companylist where id=?',$company_id)->hash;
    if ($hash->{id}){
        return $hash;
    }else{
        #此处应该在上Mojo时改成render JSON接口渲染
        #向前端发出报错。
        return 0;
    }
}

sub add_company_info {
    my $user = shift;
    my $company_id = shift; #公司编码
    my $company_name = shift; #公司名称
    my $company_address = shift; #公司地址
    my $company_tel = shift; #公司联系方式
    my $enable = shift||1;
    if (&check_005($company_id,$user)){
        my $results = $db->query('select sbu_id,userrole from admin_account where username=?',$user)->array;
        my $sbu = $results->[0];
        my $role = $results->[1];
        $db->query('insert into sbu_company_relation (company_id,sbu,role) values (?,?,?)',$company_id,$sbu,$role);
        $db->query('insert into companylist (id,name,address,tel,enable) values (?,?,?,?,?)',$company_id,$company_name,$company_address,$company_tel,$enable);
        print "公司信息成功添加"; #替换为render渲染
    }else{
        print "该公司信息已注册到事业部名下，请重新查询"； #替换为render渲染
    }
}

sub edit_company_info {
    my $company_id = shift; #公司编码
    my $company_name = shift; #公司名称
    my $company_address = shift; #公司地址
    my $company_tel = shift; #公司联系方式
    my $enable = shift;

    $@ = '';
    eval{
        my $tx = $db->begin;
        $db->query('update companylist set name=? where id=?',$company_name,$company_id) if $company_name;
        $db->query('update companylist set address=? where id=?',$company_address,$company_id) if $company_address;
        $db->query('update companylist set tel=? where id=?',$company_tel,$company_id) if $company_tel;
        $db->query('update companylist set enable=? where id=?',$enable,$company_id) if $enable;
        $tx->commit;
    };
    print $@ if $@;

    print "该公司信息已成功修改"； #替换为render渲染
}

sub delete_company_info {
    my $company_id = shift;
    my $user = shift; #在Mojo中应从session中获取
    my $user_info = $db->query('select sbu_id,userrole from admin_account where username=?',$user)->array;
    my $result = $db->query('select sbu from sbu_company_relation where company_id=?',$company_id)->array;
    $db->query('delete from sbu_company_relation where company_id=? and sbu=? and role=?',$company_id,$user_info->[0],$user_info->[1]);
    if ($result->[0]){
      if($result->[1]){
          $@ = '';
          eval{
              my $tx = $db->begin;
              $db->query('update maclist set chuli=0,xitongchuli=0,guanliyuanshenhe=2 where company_id=?,tibaoren=?',$company_id,$user);
              $db->query('update vpn_account set chuli=0,xitongchuli=0,guanliyuanshenhe=2 where company_id=?,tibaoren=?',$company_id,$user);
              $tx->commit;
          };
          print $@ if $@;
      }else{
          $@ = '';
          eval{
              my $tx = $db->begin;
              $db->query('update companylist set enable=0 where company_id=?',$company_id);
              $db->query('update maclist set chuli=0,xitongchuli=0,guanliyuanshenhe=2 where company_id=?',$company_id);
              $db->query('update vpn_account set chuli=0,xitongchuli=0,guanliyuanshenhe=2 where company_id=?',$company_id);
              $tx->commit;
          };
          print $@ if $@;
      }
      print "提交成功，请等待系统处理"; #在Mojo中render渲染
    }else{
        print "对不起，你没有删除权限"; # 在Mojo中render渲染
    }
}

